<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Authors	: Tung, Nguyen Dam                                        |
// | Email		: mask_final@yahoo.com									  | 								
// | Mobile		: (+84) 936 885 466                                       |
// | Date		: 06/2011												  |							
// +----------------------------------------------------------------------+
// | Copyrights (C) 2011                                                  |
// | All rights reserved                                                  |
// +----------------------------------------------------------------------+


class Template{
		public $ci;		
		public $template;
		public $template_data = array();
		public $name;
		public $admin;
		var $is_module = false;
		var $default_view = false;
		
		public function __construct(){
			// DIRECTORY SEPARATOR
			define('DS', DIRECTORY_SEPARATOR);	
			
			// Get CI Instance
			$this->ci =& get_instance();
			
			// Load DB
			$this->ci->load->database();
			
		}	
		
		// Set variable
		function set($name, $value){
			$this->template_data[$name] = $value;
		}
		
		
		// Load template
		function load($view = '' , $view_data = array(), $return = FALSE)
		{				
			// Get regions
			$regions = str_replace('.html', '', strtolower($this->ci->uri->segment(1)));
						
			switch($regions){
				case "admin":
					$sql = "SELECT name 
						FROM ci__templates 
						WHERE regions='backend' AND active=1
						LIMIT 1
						";	
							
					break;
				
				default:
					$sql = "SELECT name 
						FROM ci__templates 
						WHERE regions='frontend' AND active=1 
						LIMIT 1
						";	
										
					break;		
			}
			
			$query = $this->ci->db->query($sql) or die("Error: " . mysql_error());			

			foreach($query->result_array() as $row){
				$this->name = $row['name'];
			}	
			
			if($this->default_view != true){				
				$this->template = '..' . DS . '..' . DS . '..' . DS . 'templates' . DS . $this->name . DS . 'index.php';
			}else{
				$this->template = '..' . DS . '..' . DS . 'templates' . DS . $this->name . DS . 'index.php';
			}			
			
			$php_self = explode('index.php', strtolower($_SERVER['PHP_SELF']));
			$baseURL = 'http://' . $_SERVER['HTTP_HOST'] . $php_self[0];
			// Set baseURL
			$this->set('baseURL', $baseURL);			
			// Set template dir
			$this->set('base', $baseURL . 'templates' . '/' . strtolower($row['name']));
					
			// Set template dir
			$this->set('template', $baseURL . 'templates' . '/' . strtolower($row['name']));
			
			// Set content				
			if(!$this->is_module){
				if(!empty($view)){		
					// Check load module
					$checkModule = explode("module", strtolower($view));
					if(count($checkModule) >= 2){
						$this->set('contents', $this->ci->load->view('../../' . $view, $view_data, TRUE));	
					}else{									
						$this->set('contents', $this->ci->load->view($view, $view_data, TRUE));	
					}	
				}else{
					$this->set('contents', '');
				}
			}		
						
			return $this->ci->load->view($this->template, $this->template_data, $return);
		}
		
		function block($block_name){
			$ci =& get_instance();
			$ci->block->name = $block_name;
			$dir = 'blocks/block_' . $block_name . '/block_' . $block_name . EXT;
			if(file_exists($dir)){
				include($dir);
			}
		}
}
